window.onload = function () {
    changePattern();
    var content = document.getElementsByClassName("content");
    let file = document.getElementById("sendphoto");
    var hide = document.getElementsByClassName("hide");
    var img = document.getElementsByClassName("me");
    var photo=document.getElementById("photo");
    var change = document.getElementById("change");
    var obj = new Object;
    obj = getSession("data");
    //可用于判断是否存储有用户信息，若无则为游客，有则为用户
    if (obj != null) {
        content[0].innerHTML = obj.id;
        content[1].value = obj.name;
        let str=obj.phone.slice(0,3);
        let str2=obj.phone.slice(8);
        let phone=str.concat("*****",str2);
        content[5].innerHTML =phone;
        hide[0].style.display = "inline-block";
        hide[1].style.display = "inline-block";
        if (obj.photo) {
            img[0].src = "http://www.rushmc.top/" + obj.photo;
            img[1].src = "http://www.rushmc.top/" + obj.photo;
        }
    }else{
        photo.innerHTML=""
        change.innerHTML=""; 
    }
    // 监听input的file变化值
    file.onchange = function (event) {
        let file = event.target.files[0];
       
        ajax({
            type: 'post',
            url: 'http://www.rushmc.top/api/updateAvatar',
            data: {
                id: obj.id,
                avatar: file
            },
            success: function (response) {
                if (response.code == 200) {
                    hide[0].style.display = "inline-block";
                    hide[1].style.display = "inline-block";
                    img[0].src = "http://www.rushmc.top/" + response.data;
                    img[1].src = "http://www.rushmc.top/" + response.data;
                    obj.photo=response.data;
                    setSession("data",obj);
                    photo.style.color="red";
                    photo.innerHTML="更换成功！";
                    setTimeout(() => {
                        photo.style.color="#0f9cf5";
                        photo.innerHTML="点击更换头像";
                    }, 1000);
                }
            }
        })
    }
    var changepassword = document.getElementById("changepassword");
    var password = document.getElementById("password");
    var cancel = document.getElementById("cancel");
    var confirm = document.getElementById("confirm");
    var warm = document.getElementsByClassName("warm");
    var tip = document.getElementById("tip");
    change.onclick = function () {
        password.style.display = "none";
        changepassword.style.display = "block";
    }
    cancel.onclick = function () {
        password.style.display = "block";
        changepassword.style.display = "none";
        content[2].value="";
        content[3].value="";
        content[4].value="";
        warm[0].innerHTML="";
        warm[1].innerHTML="";
        warm[2].innerHTML=""
    }
    var flag = 0;
    content[2].onblur = function () {
        if (content[2].value != obj.password) {
            warm[0].style.color = "red";
            warm[0].innerHTML = "输入错误!";
            flag = 0;
        } else {
            warm[0].style.color = "green";
            warm[0].innerHTML = "正确!";
            flag = 1;
        }
    }
    var K_reg = /\w{6}/;
    content[3].onblur=function(){
        if(!K_reg.test(content[3].value)){
            warm[1].innerHTML = "密码至少为6位有效数字字母";
            warm[1].style.color = "red";
        }else{
            warm[1].innerHTML = "";
        }
    }
    confirm.onclick = function () {
        if (flag == 0) {
            return;
        }
        else if(!K_reg.test(content[3].value)){
            return 
        }
        else if (content[3].value != content[4].value) {
            warm[2].innerHTML = "密码输入不一致";
            warm[2].style.color = "red";
            return;
        }
        else if (content[3].value == obj.password) {
            warm[1].innerHTML = "密码不能和原密码相同";
            warm[1].style.color = "red";
            return;
        } else {
            ajax({
                type: "post",
                url: "http://www.rushmc.top/api/passwordUpdate",
                data: {
                    id: obj.id,
                    newPassword: content[3].value
                },
                success: function (response) {
                    if (response.code == 200) {
                        tip.innerHTML = "修改" + response.msg;
                        password.style.display = "block";
                        changepassword.style.display = "none";
                        obj.password = content[3].value;
                        for (let i = 0; i < warm.length; i++) {
                            warm[i].innerHTML = "";
                        }
                        for (let i = 2; i < 5; i++) {
                            content[i].value = "";
                        }
                        setSession("data", obj);
                    }
                }
            })
        }
    }
    var up=document.getElementById("up");
    moveup(up);
}
